// components/select/select.js
let selecteds = []
Component({
  /**
   * 组件的属性列表
   */
 
  properties: {
    current:{
      type:Object
    }
  },
  options: {
    addGlobalClass: true
  },
  ready (e) {
    setTimeout(()=>{
      let arrays = []
      arrays.push({ select: '请选择',key: -1 })
      this.setData({
        publicObj: this.properties.current
      })
      if (this.data.current.selected.length <= 0) {
        this.setData({
          'publicObj.selected': arrays
        })
      }
      this.animation = wx.createAnimation()
    }, 1000)
  },
  /**
   * 组件的初始数据
   */
  data: {
    select:'请选择',
    publicObj:{},
    index:0,
    pageLists: []
  },
  /**
   * 组件的方法列表
   */
  methods: {
    select(e) {
      let _public = this.data.publicObj
      let that = e.currentTarget.dataset
      let pages= _public.page
      let indexs = that.index
      this.data.pageLists = _public.pageList[indexs].childs.length > 0 ? _public.pageList[indexs].childs : []
      if (_public.quantity <= 1) {
        if (this.data.index == 0) {
          pages = 0
          _public.selected = []
          this.data.index = this.data.index + 1
        }
      } else if (_public.quantity == 2) {
        if (this.data.index ==0) {
          pages = 0
          _public.selected = []
          this.data.index = this.data.index + 1
        } else if (this.data.index == 1){
          pages = 1
          _public.selected.splice(this.data.index,1)
          this.data.index = this.data.index + 1
        }
      } else if (_public.quantity == 3) {
        if (this.data.index ==0) {
          pages = 0
          _public.selected = []
          this.data.index = this.data.index + 1
        } else if (this.data.index == 1){
          pages = 1
          _public.selected.splice(this.data.index,1)
          this.data.index = this.data.index + 1
        } else if (this.data.index == 2) {
          pages = 2
          _public.selected.splice(this.data.index,1)
          this.data.index = this.data.index + 1
        }
      }
      _public.selected[pages]={select:that.item,key:that.index,id:that.id}
        this.setData({
          page: pages
        })
        // if (_public.quantity === pages + 1) {
        //   this.triggerEvent('parentEvent', _public.selected)
        //   this.close()
        // } else {
        //   ++pages
        // }
        if (pages < _public.quantity) {
          ++pages
        } else {
          this.triggerEvent('parentEvent', _public.selected)
          this.close()
          this.data.pageLists = _public.pageList
        }
        let arrays = []
        for (let e of _public.selected) {
          arrays.push(e)
        }
        if (_public.selected.length < _public.quantity) {
          arrays.push({
            select: '请选择',
            key: -1
          })
        }
        this.setData({
          'publicObj.selected': arrays,
          'publicObj.pageList': this.data.pageLists,
          "publicObj.page": pages
        })
    },
    choice (e) {
      let _public = this.data.publicObj
      let that = e.currentTarget.dataset
      let list = _public.data
      let array= _public.selected
      for (let i = 0; i < that.index;i++){
        list = list[array[i].key].childs
      }
      this.setData({
        'publicObj.pageList': list,
        'publicObj.page': that.index,
        index: that.index
      })
    },
    close () {
      this.setData({
        'current.show': false
      })
    }
  }
})
